package com.gym.web.controller.chat.controller;

import java.util.List;

import com.gym.common.core.domain.entity.SysUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gym.common.annotation.Log;
import com.gym.common.enums.BusinessType;
import com.gym.system.chat.domain.GymChat;
import com.gym.system.chat.service.IGymChatService;
import com.gym.common.core.controller.BaseController;
import com.gym.common.core.domain.AjaxResult;
import com.gym.common.utils.poi.ExcelUtil;
import com.gym.common.core.page.TableDataInfo;

/**
 * 评论Controller
 *
 * @author administrator
 * @date 2021-04-11
 */
@Controller
@RequestMapping("/chat/chat")
public class GymChatController extends BaseController {
    private String prefix = "chat/chat";

    @Autowired
    private IGymChatService gymChatService;

    @RequiresPermissions("chat:chat:view")
    @GetMapping()
    public String chat() {
        return prefix + "/chat";
    }

    /**
     * 查询评论列表
     */
    @RequiresPermissions("chat:chat:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GymChat gymChat) {
        startPage();
        List<GymChat> list = gymChatService.selectGymChatList(gymChat);
        return getDataTable(list);
    }

    /**
     * 导出评论列表
     */
    @RequiresPermissions("chat:chat:export")
    @Log(title = "评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GymChat gymChat) {
        List<GymChat> list = gymChatService.selectGymChatList(gymChat);
        ExcelUtil<GymChat> util = new ExcelUtil<GymChat>(GymChat.class);
        return util.exportExcel(list, "chat");
    }

    /**
     * 新增评论
     */
    @GetMapping("/add")
    public String add(Model model) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("user", sysUser);
        return prefix + "/add";
    }

    /**
     * 新增保存评论
     */
    @RequiresPermissions("chat:chat:add")
    @Log(title = "评论", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GymChat gymChat) {
        return toAjax(gymChatService.insertGymChat(gymChat));
    }

    /**
     * 修改评论
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        GymChat gymChat = gymChatService.selectGymChatById(id);
        mmap.put("gymChat", gymChat);
        return prefix + "/edit";
    }

    /**
     * 修改保存评论
     */
    @RequiresPermissions("chat:chat:edit")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GymChat gymChat) {
        return toAjax(gymChatService.updateGymChat(gymChat));
    }

    /**
     * 删除评论
     */
    @RequiresPermissions("chat:chat:remove")
    @Log(title = "评论", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(gymChatService.deleteGymChatByIds(ids));
    }
}
